package mipo.top.shop.service.impl;

import mipo.top.admin.domain.Employee;
import mipo.top.admin.domain.Shop;
import mipo.top.admin.service.IEmployeeService;
import mipo.top.admin.service.IShopService;
import mipo.top.base.Dto.Point;
import mipo.top.base.exception.BusinessException;
import mipo.top.base.query.BaseQuery;
import mipo.top.base.service.impl.BaseServiceimpl;
import mipo.top.base.util.*;
import mipo.top.shop.Dto.AcceptDto;
import mipo.top.shop.domain.OrderPetAcquisition;
import mipo.top.shop.domain.SearchMasterMsg;
import mipo.top.shop.domain.SearchMasterMsgAuditLog;
import mipo.top.shop.mapper.SearchMasterMsgAuditLogMapper;
import mipo.top.shop.mapper.SearchMasterMsgMapper;
import mipo.top.shop.query.SearchMasterMsgQuery;
import mipo.top.shop.service.IOrderPetAcquisitionService;
import mipo.top.shop.service.ISearchMasterMsgService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class SearchMasterMsgServiceimpl extends BaseServiceimpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private IShopService shopService;
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IOrderPetAcquisitionService orderPetAcquisitionService;
    @Override
    public void publish(SearchMasterMsg searchMasterMsg) {
        Date date = new Date();
        searchMasterMsgMapper.save(searchMasterMsg);
        StringBuffer buffer = getStringBuffer(searchMasterMsg,date);
        NotNullCheck(searchMasterMsg,date);
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        List<Shop> shops = shopService.findall();
        DistanceUtil.getNearestShop(point,shops);
        searchMasterMsg.setState(1);
        searchMasterMsgMapper.update(searchMasterMsg);
    }

    private void NotNullCheck(SearchMasterMsg searchMasterMsg,Date date) {
        if(StringUtils.isEmpty(searchMasterMsg.getName())||StringUtils.isEmpty(searchMasterMsg.getAddress())){
            throw new BusinessException("参数为空，请重新提交");
        }

    }

    @NotNull
    private StringBuffer getStringBuffer(SearchMasterMsg searchMasterMsg,Date date) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(searchMasterMsg.getName())
                .append(searchMasterMsg.getAddress())
                .append(searchMasterMsg.getNote());
        if (!BaiduAuditUtils.TextCensor(buffer.toString())){
            SearchMasterMsgAuditLog searchMasterMsgAuditLog = new SearchMasterMsgAuditLog();
            searchMasterMsgAuditLog.setMsgId(searchMasterMsg.getId());
            searchMasterMsgAuditLog.setAuditTime(date);
            searchMasterMsgAuditLog.setNote("百度AI审核为参数非法，请重新输入");
            searchMasterMsgAuditLogMapper.save(searchMasterMsgAuditLog);
            throw new BusinessException("参数非法，请重新输入");
        }
        return buffer;
    }

    public PageUtil<SearchMasterMsg> queryByStateData(SearchMasterMsgQuery query) {
        Integer count = searchMasterMsgMapper.queryByStateDataCount(query);
        List<SearchMasterMsg> rows = searchMasterMsgMapper.pageQueryByStateData(query);
        return new PageUtil(count,rows);
    }

    @Override
    public JsonResult reject(Long id, Employee employee) {
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.findone(id);
        if (searchMasterMsg==null){
            throw new BusinessException("订单不存在");
        }
        searchMasterMsg.setShopId(null);
        searchMasterMsgMapper.update(searchMasterMsg);
        return null;
    }

    @Override
    public void accept(AcceptDto acceptDto) {
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.findone(acceptDto.getMsgId());
        if (searchMasterMsg==null){
            throw new BusinessException("订单不存在");
        }
        synchronized (searchMasterMsg.getId()){
            Employee employee = employeeService.findone(acceptDto.getHandler());
            if (employee==null){
                throw new BusinessException("员工不存在");
            }
            if(searchMasterMsg.getState()==1&&searchMasterMsg.getShopId()==employee.getShopId()){
                searchMasterMsg.setState(2);
                searchMasterMsgMapper.update(searchMasterMsg);
                OrderPetAcquisition orderPetAcquisition = new OrderPetAcquisition();
                orderPetAcquisition.setOrderSn(CodeGenerateUtils.generateOrderSn(acceptDto.getHandler()));
                orderPetAcquisition.setDigest(acceptDto.getNote());
                orderPetAcquisition.setLastConfirmTime(new Date(System.currentTimeMillis()+3*24*60*60*1000));
                orderPetAcquisition.setState(0);
                orderPetAcquisition.setPrice(new BigDecimal(searchMasterMsg.getPrice()));
                orderPetAcquisition.setUserId(searchMasterMsg.getId());
                orderPetAcquisition.setShopId(searchMasterMsg.getShopId());
                orderPetAcquisition.setAddress(searchMasterMsg.getAddress());
                orderPetAcquisition.setEmpId(employee.getId());
                orderPetAcquisition.setSearchMasterMsgId(searchMasterMsg.getId());
                orderPetAcquisitionService.save(orderPetAcquisition);
            }
        }
    }

    @Override
    public void takePet(Long id, Employee employee) {
        Long shopId = employee.getShopId();
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.findone(id);
        if (shopId==null){
            throw new BusinessException("非店铺管理员无法操作");
        }
        searchMasterMsg.setShopId(shopId);
        if(BaiduAuditUtils.TextCensor(searchMasterMsg.getName())){
            searchMasterMsg.setState(0);
            searchMasterMsgMapper.update(searchMasterMsg);
            throw new BusinessException("宠物名称非法");
        }
        if(BaiduAuditUtils.ImgCensor(searchMasterMsg.getResources())){
            searchMasterMsg.setState(0);
            searchMasterMsgMapper.update(searchMasterMsg);
            throw new BusinessException("宠物图片非法");
        }
        searchMasterMsgMapper.update(searchMasterMsg);
    }
}
